package vista;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import modelo.*;
import modelo.dao.Servidor;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class DlgSeleccionarEmpleado extends javax.swing.JDialog {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JTable tbEmpleados;
	private JScrollPane jScrollPane1;
	private JButton btAceptar;
	private JButton btSalir;
	private ArrayList<Empleado> empleados;

	public DlgSeleccionarEmpleado(JDialog dialog) {
		super(dialog);
		initGUI();
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setTitle("Seleccionar Empleado");
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(34, 23, 369, 158);
					{
						empleados = Servidor.empleados().consultarEmpleadosPorStatus(Status.ACTIVO);
						
						AbstractTableModel tbEmpleadosModel = new AbstractTableModel() {
							@Override
							public Object getValueAt(int rowIndex, int columnIndex) {
								Empleado emp = empleados.get(rowIndex);
								
								switch (columnIndex) {
								case 0: return emp.getCedula();
								case 1: return emp.getNombre();
								case 2: return emp.getApellido();
								}
								
								return null;
							}
							
							@Override
							public int getRowCount() {
								return empleados.size();
							}
							
							@Override
							public String getColumnName(int columnIndex) {
								switch (columnIndex) {
								case 0: return "Cedula";
								case 1: return "Nombre";
								case 2: return "Apellido";
								}
								
								return null;
							}
							
							@Override
							public int getColumnCount() {
								return 3;
							}
						};
						
						tbEmpleados = new JTable();
						jScrollPane1.setViewportView(tbEmpleados);
						tbEmpleados.setModel(tbEmpleadosModel);
						tbEmpleados.setBounds(163, 69, 243, 75);
					}
				}
				{
					btSalir = new JButton();
					getContentPane().add(btSalir);
					btSalir.setText("Salir");
					btSalir.setIcon(new ImageIcon(getClass().getClassLoader()
							.getResource("iconos/Salir.gif")));
					btSalir.setHorizontalTextPosition(JButton.CENTER);
					btSalir.setVerticalTextPosition(JButton.BOTTOM);
					btSalir.setBounds(343, 198, 79, 40);
				}
				{
					btAceptar = new JButton();
					getContentPane().add(btAceptar);
					btAceptar.setText("Aceptar");
					btAceptar.setIcon(new ImageIcon(getClass().getClassLoader()
							.getResource("iconos/Aceptar.png")));
					btAceptar.setHorizontalTextPosition(JButton.CENTER);
					btAceptar.setVerticalTextPosition(JButton.BOTTOM);
					btAceptar.setBounds(236, 198, 79, 40);
				}
			}
			this.setSize(460, 295);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private DlgSeleccionarEmpleado dsemp = this;
	private Empleado empleado;

	public Empleado mostrar(JDialog dialog) {
		setLocationRelativeTo(null);

		empleado = null;
		
		btAceptar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (tbEmpleados.getSelectedRow() != -1) {
					empleado = empleados.get(tbEmpleados.getSelectedRow());
					dsemp.dispose();
				} else
					JOptionPane.showMessageDialog(dsemp,
							"Debe Seleccionar un Empleado", "Seleccionar",
							JOptionPane.ERROR_MESSAGE);
			}
		});

		btSalir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dsemp.dispose();
			}
		});

		setModalityType(ModalityType.APPLICATION_MODAL);
		setVisible(true);
		
		return empleado;
	}

}
